package com.nlecloud.bc.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.nlecloud.bc.interceptor.AuthInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @author warrior
 * @description 配置拦截器路径
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${file-save-path}")
    private String fileSavePath;

    @Resource
    private ObjectMapper objectMapper;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AuthInterceptor(objectMapper))
                //添加需要认证拦截请求的路径
                .addPathPatterns("/**")
                //添加放开认证请求路径
                .excludePathPatterns("/user/login/**","/user/register/**","/uploadFile/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         * 配置资源映射
         * 意思是：如果访问的资源路径是以“/images/”开头的，
         * 就给我映射到本机的“E:/images/”这个文件夹内，去找你要的资源
         * 注意：E:/images/ 后面的 “/”一定要带上
         */
        registry.addResourceHandler("/uploadFile/**").addResourceLocations("file:"+fileSavePath);
    }
}
